import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'mobx-react';
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import { asyncComponent } from 'react-async-component';
import { Tools, Bubble } from 'weaver-mobile';

import allStore from './stores';
import './style';

const getLabel = Tools.getLabel;
const coms = {
  YulinEntrance: asyncComponent({
    resolve: () => import("./components/yulinEntrance")
  }),
}
class Root extends React.Component {
  getLocaleLabel = Tools.getLocaleLabel;
  constructor(props) {
    super(props);
    this.state = {
      loadLang: false
    }
    this.getLocaleLabel.bind(this);
  }

  componentDidMount() {
    Tools.ssoInit().then(() => {
      this.getLocaleLabel('common', () => {
        this.setState({ loadLang: true });
      });
    }).catch((res) => {
      Bubble.error(getLabel(386481, '登录失败') + "," + JSON.stringify(res), 0);
    });

  }

  render() {
    return (
      <Provider {...allStore}>
        <Router>
          <Route
            name="home"
            path="/"
            render={
              (props) => {
                if (!this.state.loadLang) {
                  return <div></div>
                } else {
                  return (
                    <Switch>
                      <Route name="yulin" path="/yulin-entrance" component={coms.YulinEntrance} />
                      <Redirect to="/yulin-entrance" />
                    </Switch>
                  )
                }
              }
            }
          />
        </Router>
      </Provider>
    )
  }
}

render(<Root />, document.getElementById('root'));